package com.yonyou.pmclouds.constructionlog.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.completedcollate.rmiitf.CompletedCollateQuery;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.constructionlog.rmiitf.ConstructionLogQuery;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.inspectitem.rmiitf.InspectItemService;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectBodyVO;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectVO;
import com.yonyou.pmclouds.processinspect.rmiitf.ProcedureInspectRemoteService;
import com.yonyou.pmclouds.team.rmiitf.TeamEnterpriseQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Slf4j
@Controller
@RequestMapping(value = "/constructionlog/ref")
public class ConstructionLogRefController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ConstructionLogQuery constructionLogQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProcedureInspectRemoteService procedureInspectService;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private CompletedCollateQuery collateQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private InspectItemService inspectItemService;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private TeamEnterpriseQuery enterpriseQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;

    @Autowired
    private UserInfoQuery userQuery;

    private static final String[] USER_ID_FIELDS = {ConstructionLogVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {ConstructionLogVO.CREATOR_NAME};

    /**
     * 根据pk获取施工记录数据
     *
     * @param pkBill
     * @return com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO
     * @author yzx
     * @date 2018/5/29 20:22
     */
    @RequestMapping(value = "/getConstructionLog")
    public ConstructionLogVO getConstructionLog(String pkBill) throws BusinessException {
        if (StringUtils.isEmpty(pkBill)) {
            return new ConstructionLogVO();
        }
        ConstructionLogVO constructionLog = constructionLogQuery.queryOne(pkBill);
        // 填充检查项内容
        InspectItemVO[] inspectitems = constructionLog.getInspectitems();
        String[] inspectItemPks = new String[inspectitems.length];
        for (int i = 0; i < inspectitems.length; i++) {
            inspectItemPks[i] = inspectitems[i].getPk_inspectitem();
        }
        InspectItemVO[] inspectItemVOS = inspectItemService.queryInspectItemsInfoByPks(inspectItemPks);
        for (InspectItemVO inspectItemVO : inspectItemVOS) {
            if (inspectItemVO.getDatas() == null) {
                inspectItemVO.setDatas(new Double[0]);
            }
            if (inspectItemVO.getPictures() == null) {
                inspectItemVO.setPictures(new String[0]);
            }
        }
        constructionLog.setInspectitems(inspectItemVOS);
        // 创建人赋值
        userQuery.tranUserInfoField(new ConstructionLogVO[]{constructionLog}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return constructionLog;
    }

    /**
     * 根据pk获取工序报验数据
     *
     * @param pkBill
     * @return com.yonyou.pmclouds.processinspect.entity.ProcedureInspectVO
     * @author yzx
     * @date 2018/5/29 20:23
     */
    @RequestMapping(value = "/getProcedureInspect")
    public ProcedureInspectVO getProcedureInspect(String pkBill) throws BusinessException {
        if (StringUtils.isEmpty(pkBill)) {
            return new ProcedureInspectVO();
        }
        ProcedureInspectVO procedureInspectVO = getProcedureInspectVO(pkBill);
        return procedureInspectVO;
    }

    private ProcedureInspectVO getProcedureInspectVO(String pkBill) throws BusinessException {
        ProcedureInspectVO procedureInspectVO = procedureInspectService.queryPrcInspectDetail(pkBill);

        userQuery.tranUserInfoField(new ProcedureInspectVO[]{procedureInspectVO},
                new String[]{ProcedureInspectVO.CREATOR, ProcedureInspectVO.ACCEPTOR},
                new String[]{ProcedureInspectVO.CREATORNAME, ProcedureInspectVO.ACCEPTORNAME});
        userQuery.tranUserInfoField(procedureInspectVO.getBodyVOs(),
                new String[]{ProcedureInspectBodyVO.RECORD_CREATOR},
                new String[]{ProcedureInspectBodyVO.RECORD_CREATOR_NAME});
        return procedureInspectVO;
    }
}
